package face.pay.util;


import java.security.MessageDigest;
import java.util.UUID;

public class MD5Utils {


    public static String string2MD5(String inStr,String key){
        MessageDigest md5 = null;

        try{
            md5 = MessageDigest.getInstance("MD5");
            md5.update(key.getBytes());
        }catch (Exception e){
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++){
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr){

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++){
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }


    public static String encryption(String content,String key){
        byte[] contentBytes = content.getBytes();
        byte[] keyBytes = key.getBytes();

        byte dkey = 0;
        for(byte b : keyBytes){
            dkey ^= b;
        }

        byte salt = 0;  //随机盐值
        byte[] result = new byte[contentBytes.length];
        for(int i = 0 ; i < contentBytes.length; i++){
            salt = (byte)(contentBytes[i] ^ dkey ^ salt);
            result[i] = salt;
        }
        return   EncodeTools.parseByte2HexStr(result);
    }

    public static byte[] decipher(String content,String key){
        byte[] contentBytes = EncodeTools.parseHexStr2Byte(content);
        byte[] keyBytes = key.getBytes();

        byte dkey = 0;
        for(byte b : keyBytes){
            dkey ^= b;
        }

        byte salt = 0;  //随机盐值
        byte[] result = new byte[contentBytes.length];
        for(int i = contentBytes.length - 1 ; i >= 0 ; i--){
            if(i == 0){
                salt = 0;
            }else{
                salt = contentBytes[i - 1];
            }
            result[i] = (byte)(contentBytes[i] ^ dkey ^ salt);
        }
        return result;
    }

    // 测试主函数
    public static void main(String args[]) {



        String s = "100000|1234567654";
        String key = "1212";
        System.out.println("原始：" + s);
        System.out.println("MD5后：" + string2MD5(s,key));
        String en = convertMD5(s);
        System.out.println("加密后：" + en);
        System.out.println("解密后：" + convertMD5(en));

        String k= encryption(s,key);
        System.out.println("再次加密：" + k);

        System.out.println("再次加密：" + convertMD5(k));
        System.out.println("再次加密：" + convertMD5(convertMD5(k)));

        decipher(k,key);
      /*
        System.out.println("加密后："+ k);

        String s3 =   new String(decipher(k,key));

        System.out.println("解后："+ s3);*/


    }

}
